package com.wu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.wu.dao.UserDao;
import com.wu.pojo.po.orgPo.OrgBase;
import com.wu.pojo.res.Result;
import com.wu.pojo.po.userPo.User;
import com.wu.service.OrgBaseService;
import com.wu.service.UserService;
import com.wu.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;
import java.util.Map;


@Service
@Slf4j
@CacheConfig(cacheNames = "userDao")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private OrgBaseService orgBaseService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Cacheable
    public User selectOne(User user) {
        User one = userDao.queryOne(user);
        return one;
    }

    @Override
    @Cacheable
    public List<User> selectList(User user) {
        List<User> users = userDao.queryList(user);
        return users;
    }

    @Override
    @Cacheable
    public List<User> selectListByMap(Map map) {
        return userDao.queryListByMap(map);
    }

    @Transactional
    @Override
    public boolean insertUser(User user) {
        if (null == user || "".equals(user.getOrgId())) {
            throw new RuntimeException("c:插入用户失败，用户id不能为空！");
        }
        try {
            int i = userDao.insertUser(user);
            if (i == 1) {
                return true;
            } else {
                throw new RuntimeException("a:插入用户失败！" + user);
            }
        } catch (Exception e) {
            throw new RuntimeException("b:插入用户失败：" + e.getMessage());
        }
    }

    @Transactional
    @Override
    public boolean updateUser(User user) {
        if (null == user || "".equals(user.getOrgId())) {
            throw new RuntimeException("c:更新用户失败，用户id不能为空！");
        }
        try {
            int i = userDao.updateUser(user);
            if (i == 1) {
                return true;
            } else {
                throw new RuntimeException("a:更新用户失败！" + user);
            }
        } catch (Exception e) {
            throw new RuntimeException("b:更新用户失败：" + e.getMessage());
        }
    }

    @Transactional
    @Override
    @CacheEvict(keyGenerator = "deleteByIdKeyGenerator")
    public boolean deleteUser(String id) {
        if (id != null && !"".equals(id)) {
            try {
                int i = userDao.deleteUser(id);
                if (i == 1) {
                    return true;
                } else {
                    throw new RuntimeException("a:删除用户失败！" + id);
                }
            } catch (Exception e) {
                throw new RuntimeException("b:删除用户失败：" + e.getMessage());
            }
        } else {
            throw new RuntimeException("c:删除用户失败，用户id不能为空！");
        }
    }

    //注册验证
    public Result register(User user, OrgBase orgBase){

        //user校验合法
        List<User> user4 = selectList(new User().setOrgId(orgBase.getOrgId()));
        if (user4.size() > 0) {
            return Result.fail("该企业已存在用户，请勿重复创建！");
        }
        List<User> user2 = selectList(new User().setEmail(user.getMobile()));
        if (user2.size() > 0) {
            return Result.fail("手机号已被使用，请更换手机号！");
        }
        List<User> user1 = selectList(new User().setEmail(user.getEmail()));
        if (user1.size() > 0) {
            return Result.fail("邮箱已被使用，请更换邮箱！");
        }
        List<User> user3 = selectList(new User().setEmail(user.getFax()));
        if (user3.size() > 0) {
            return Result.fail("传真号已被使用，请更换传真号！");
        }

        String orgId = orgBase.getOrgId();
        if ((null == user.getUsername() || "".equals(user.getUsername()))) {
            user.setUsername(orgId);
        } else {
            user.setId(CommonUtils.getUUID().toUpperCase());
        }
        user.setOrgId(orgId);

        JSONObject data = new JSONObject();
        data.put("orgBase", orgBase);
        data.put("userPo", user);
        return Result.succ(data);
    }
}
